- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 33.6k
buffer: refactor buffer exports #13807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
        
          
                lib/buffer.js
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo
| 
 | 
        
          
                lib/buffer.js
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer using buf1 instead of a in the code as well.
        
          
                lib/internal/buffer.js
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would { FastBuffer: undefined } make it more self-documenting?
        
          
                lib/internal/errors.js
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alphabetical order.
| Forgot to mark this as in progress as I still need to add the documentation updates for the new error codes... | 
        
          
                lib/internal/errors.js
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call the error type something more generic? There could be any variable that may not be negative or that may not be greater than x.
        
          
                lib/internal/errors.js
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if the error codes should be more generic or not but I think it would make sense if they are. In this case the ERR_INVALID_OPT_VALUE type could also be used as such.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO including the encoding name is better.
        
          
                lib/internal/errors.js
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment below. This type is at least redundant to ERR_UNKNOWN_ENCODING but I would use ERR_INVALID_OPT_VALUE instead.
        
          
                lib/internal/errors.js
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if these types would only fit for buffers and therefore I recommend to remove the BUFFER part from the name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's the whole over specialization of invalid options debate:
try {
  // do some stuff
  var f = fs.openSync('g.txt', 'rgh');
  // do other stuff
catch (e) {
  if (e.code == 'ERR_INVALID_OPT_VALUE') console.error(e) && process.exit(1) // because it's a programming error
  else ...;
}Maybe worth creating a new type? OptionError, or have all the codes include a substring?
        
          
                lib/internal/errors.js
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be consolidated with ERR_ARG_OUT_OF_BOUNDS even though it's not as specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overspecialization of error codes should be discussed
        
          
                lib/internal/errors.js
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's the whole over specialization of invalid options debate:
try {
  // do some stuff
  var f = fs.openSync('g.txt', 'rgh');
  // do other stuff
catch (e) {
  if (e.code == 'ERR_INVALID_OPT_VALUE') console.error(e) && process.exit(1) // because it's a programming error
  else ...;
}Maybe worth creating a new type? OptionError, or have all the codes include a substring?
| Over-generalization of error codes will make those fairly useless and users will end up parsing the error messages again to find out what actually happened. There's little harm in having specific error codes. | 
| 
 I suggested two solutions: Either create an  | 
| Superseded by #13976 | 
| This is not fully superseded. I will refactor. Please do not close. | 
* Move to more efficient module.exports pattern * Refactor requires * Eliminate circular dependency on internal/buffer * Add names to some functions * Fix circular dependency error in assert.js
| Updated. I was waiting for #13976 to land so I could drop the internal/errors commit and refactor this. It should be good to go now. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would like to see obfuscated whiles replaced with for...
| var i = 0; | ||
| while (++i < byteLength && (mul *= 0x100)) | ||
| val += this[offset + i] * mul; | ||
| var val = this[offset]; | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're churning let's get some butter: replace with for(...;...;...) to make it more readable. Performance should be on par
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to leave these for now. We can hit those in a separate PR
| So I would like to see the obfuscated  | 
| CI is green. | 
* Move to more efficient module.exports pattern * Refactor requires * Eliminate circular dependency on internal/buffer * Add names to some functions * Fix circular dependency error in assert.js PR-URL: #13807 Reviewed-By: Refael Ackermann <refack@gmail.com>
| Landed in 355523f | 
| This doesn’t land cleanly on 8.x; if you can, please follow the guide and raise a backport PR, if you don’t think it’s worth it let me know and we’ll add the  | 
| Unless it causes pain for backporting other things, this is not a priority to backport. | 
Migrate most errors to use internal/errorsrefack: crossed off, as this was done in errors, buffer: Migrate buffer errors to use internal/errors #13976Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
buffer